# !/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import time
from os import system
from datetime import datetime
def loggingdata(write_log):
    with open("test_setting.log",'a') as f:
        f.write(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
        f.write(' ' + write_log + '\n')
#重启BBB板卡
def ReBoot_BBB():
    try:
        system('reboot')
    except:
        system('echo "mt888999"|sudo -S reboot')      

    # 【1. 获取文件绝对路径】
def _getFilesPath(file_dir):
    setting_path = []
    for root, dirs, files in os.walk(file_dir,topdown=False):
        setting_path = [os.path.join(root,name,'settings')  for name in dirs]
    return setting_path
def setting_false():
    setting_path = _getFilesPath('/var/lib/connman/') 
    reboot_flag = False 
    for set_path in setting_path:
        setting_line = -1
        f = open(set_path, "r")
        all_lines = f.readlines()
        for i in range(len(all_lines)):
            if all_lines[i] == 'AutoConnect=true\n':
                setting_line = i
        f.close()
        if setting_line > 0:
            all_lines[setting_line] = 'AutoConnect=false\n'
            f1 = open(set_path, "w")
            for i in all_lines:
                f1.write(i)
            f1.close()
            reboot_flag = True
        #print(all_lines)
    loggingdata('reboot_flag = {}'.format(reboot_flag))
    if reboot_flag:
        loggingdata('reboot...')
        ReBoot_BBB()


if __name__=='__main__':
    # 延时10分钟检测
    loggingdata('start setting...')
    time.sleep(60*10)   
    setting_false()



# !/usr/bin/env python
# -*- coding: utf-8 -*-
import os
def _getFilesPath(file_dir):
    setting_path = []
    for root, dirs, files in os.walk(file_dir,topdown=False):
        setting_path = [os.path.join(root,name,'settings')  for name in dirs]
    return setting_path
def setting_ture():
    setting_path = _getFilesPath('/var/lib/connman/')  
    for set_path in setting_path:
        setting_line = -1
        f = open(set_path, "r")
        all_lines = f.readlines()
        for i in range(len(all_lines)):
            if all_lines[i] == 'AutoConnect=false\n':
                setting_line = i 
        f.close()
        if setting_line > 0:
            all_lines[setting_line] = 'AutoConnect=true\n'
            f1 = open(set_path, "w")
            for i in all_lines:
                f1.write(i)
            f1.close()
        print(all_lines)
    print(setting_path)


if __name__=='__main__':
    setting_ture()